/*
*ConfigSimulation.js
*配置模拟=>模型选择,模型参数设置 model
*/

import {routerRedux} from 'dva/router';
import api from '../api';
import Mock from 'mockjs';
export default {
	namespace : 'configSimulation',
	state : {
		CSassets:[],
		isShowTable:false,
		optimizeModel:'0',//优化模型
		optimizeRules:'',//优化规则
		timeRange:'0',//时间区间
		beginDate:'',//开始时间
		endDate:'',//结束时间
		UpOfWeight:'',//权重上限
		assetsRisk:[]
	},
	subscriptions : {
		setup({dispatch, history}) {
			history.listen(({pathname, query}) => {
				if (pathname === '/assetAllocation/assetSimulation') {
					if(!_.isEmpty(query)){
						dispatch({type: 'getConfigKpis',payload:query})
						// dispatch({type: 'CSchangeState',payload:{CSassets:[]}})
					}
				}
			})
		}
	},
	effects : {
		*getConfigKpis({payload},{call,put,select}){
			const state=yield select(state => state.configSconst);
			const response = yield call(api.construction.getConfigKpis, payload);
			yield put({
        type: 'getConfigKpisSuccess',
        payload:response ,
      });
		},
		// *getAssetRatio({payload},{call,put}){
		// 	const response = yield call(api.construction.getAssetRatio, payload);
		// 	console.log('payload>>',response)
		// 	yield put({
  //       type: 'getAssetRatioSuccess',
  //       payload:response ,
  //     });
		// },
		* CSchangeState({payload},{put}){
			yield put({
				type: 'setState',
				payload
			})
		},
		*changeRoute({payload:query},{call,put}){
			yield put(routerRedux.push({
				pathname:`/assetAllocation/assetSimulation`,
				query:{
					...query,
				}
			}))
		},
	},
	reducers : {
		setState(state, {payload}) {
			return {
				...state,
				...payload
			}
		},
		getConfigKpisSuccess(state, {payload}){
			const data = payload.data;
			const leName = state.CSassets;
			const kpisArr = [];
			const kName = '';
			leName.map((item)=>{
				for(var i in data) {
				  if(item.assetId == i){
				  	const kName = item.productName;
				  	data[i].name = kName;
				  }

				}
			})
			for(var i in data) {
				kpisArr.push(data[i])
			}
			

			return {
        ...state,
        assetsRisk:kpisArr,//各类资产的预期风险收益特征
      }
		},
		// getAssetRatioSuccess(state,{payload}){
		// 	const data = payload.data;
		// 	console.log('getConfigKpisSuccess>',data)
		// 	const mockData = Mock.mock({
		// 		"data":[
		//       {
		//         "assetCode":"code1",
		//         "assetName":"沪深300",
		//       },
		//       {
		//         "assetCode":"code2",
		//         "assetName":"企业债指数",
		//       },
		     
		//     ],
		// 	});
		// 	const kpisArr = [];
		// 	const kName = '';
		// 	const kArr = mockData.data;
		// 	kArr.map((item)=>{
		// 		for(var i in data) {
		// 		  console.log('getAssetRatioSuccess',i,":",data[i],'mockData',mockData);
		// 		  if(item.assetCode == i){
		// 		  	const kName = item.assetName;
		// 		  	data[i].name = kName;
		// 		  }
		// 		}
		// 	})
		// 	for(var i in data) {
		// 		kpisArr.push(data[i])
		// 	}
		// 	console.log('getAssetRatioSuccess',data,kpisArr)

		// 	return {
  //       ...state,
  //       assetsRatio:kpisArr,//各类资产的相关系数
  //       kArr:kArr
  //     }
		// }
	}
};
